﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    public partial class AccountController
	{
        public ActionResult UserGroup()
        {
            CurrentUser.EnsureAdmin();
            return View();
        }
        public ActionResult UserGroupPermissionSave(string id)
        {
            CurrentUser.EnsureAdmin();
            string all_permissions = Request.Form["selectPermissions"];
            string[] permission_list = all_permissions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            using (var DB = new WebcarContext())
            {

                DB.sys_group_purview.Delete(a => a.group_id == id);
                foreach (var permission in permission_list)
                {
                    sys_group_purview purview = new sys_group_purview();
                    purview.group_id = id;
                    purview.purview_id = permission;
                    DB.sys_group_purview.Add(purview);
                }
                DB.SaveChanges();
            }
            return RedirectToAction("UserGroup");// View(viewName: "UserGroup");
        }
        public ActionResult UserGroupPermissions(string id)
        {
            CurrentUser.EnsureAdmin();
            var query = (from p in WebcarDataProvider.DB.sys_group_purview 
                        select p).ToList();
            var group = WebcarDataProvider.DB.USER_GROUP.Find(id);

            var result = (from p2 in PermissionManager.AllPermissions
                         join p in query.Where(a=>a.group_id==id)
                         on p2.PermissionId equals p.purview_id
                         into pp
                         
                         from bb in pp.DefaultIfEmpty()
                         select new { 
                             PermissionId=p2.PermissionId,
                             Name=p2.Name,
                             Module=p2.Module,
                             PermissionType=p2.PermissionType ,
                             Selected = bb==null?false:true
                         }).ToList();
            ViewBag.SelectedRowIds = result.Where(a => a.Selected).Select(a => a.PermissionId).Join(";");
            if(group!=null)
                ViewBag.GroupName = group.GROUP_NOTE;
            return View(model:result);
        }
        public ActionResult UserGroupSavePermissions(string groupId)
        {
            return PartialView();
        }

        public ActionResult EditGroup(string groupId)
        {
            CurrentUser.EnsureAdmin();
            USER_GROUP group = null;
            if(groupId.IsNotNull())
                group = WebcarDataProvider.DB.USER_GROUP.Find(groupId);
            return PartialView(group);
        }
        public ActionResult GroupDelete(string id)
        {
            CurrentUser.EnsureAdmin();
            using (var DB = new WebcarContext())
            {
                USER_GROUP group = DB.USER_GROUP.Find(id);
                DB.USER_GROUP.Remove(group);
                DB.SaveChanges();
            }
            return PartialView();
        }
        public ActionResult EditGroupSave(USER_GROUP model)
        {
            try
            {
                CurrentUser.EnsureAdmin();
                if (model.GROUP_NOTE.IsNull())
                    return Content("请输入权限组名称。");
                using (var db = new WebcarContext())
                {
                    if (model.GROUP_CODE.IsNotNull())
                    {
                        var exist = db.USER_GROUP.Find(model.GROUP_CODE);
                        model.CopyPropertyTo(exist);
                    }
                    else
                    {
                        model.GROUP_CODE = "G" + WebcarDataProvider.DB.GetSearialId();
                        db.USER_GROUP.Add(model);
                    }
                    db.SaveChanges();
                }
                return SuccessAlertThenReload();
            }
            catch (Exception ex)
            {
                return Content("错误:"+ex.Message);
            }
        }


        public ActionResult UserGroupPartialView()
        {
            return PartialView(model: GetQueryList());
        }

        IQueryable GetQueryList()
        {
            IQueryable list = from p in WebcarDataProvider.DB.USER_GROUP select p;

            return list;
        }
	}
}